package component.fulltextsearch.index;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

import component.fulltextsearch.constant.SearchConstant;
import component.fulltextsearch.entity.UserDocument;
import component.fulltextsearch.index.handlingtypes.ExtensionFileHandler;
import component.fulltextsearch.index.handlingtypes.FileHandlerException;

/**
 * 文档转换工具
 *
 */
public class DocumentTransform {
	
	private ExtensionFileHandler fileHandler = new ExtensionFileHandler();
	
	private Map cache  = new HashMap();
	
	public DocumentTransform() {
	
	}
	
	public Document transform(UserDocument userDoc) {
		Document doc = new Document();
		
		//需要进行分词搜索
		doc.add(new Field(SearchConstant.NAME_KEYVALUE, null2Blank(userDoc.getKeyValue()), Store.YES, Index.NOT_ANALYZED));
		doc.add(new Field(SearchConstant.NAME_RIGHTROLES, null2Blank(userDoc.getRightRoles()), Store.YES, Index.ANALYZED));
		doc.add(new Field(SearchConstant.NAME_RIGHTUSERS, null2Blank(userDoc.getRightUsers()), Store.YES, Index.ANALYZED));
		doc.add(new Field(SearchConstant.NAME_KEYWORDS, null2Blank(userDoc.getKeywords()), Store.YES, Index.ANALYZED));
		doc.add(new Field(SearchConstant.NAME_SUMMARY, null2Blank(userDoc.getSummary()), Store.YES, Index.ANALYZED));
		doc.add(new Field(SearchConstant.NAME_NAME, null2Blank(userDoc.getName()), Store.YES, Index.ANALYZED));
		doc.add(new Field(SearchConstant.NAME_CONTENT, null2Blank(userDoc.getContent()), Store.YES, Index.ANALYZED));		
		doc.add(new Field(SearchConstant.NAME_ATTACHMENTCONTENT, null2Blank(this.getAttachmentContent(userDoc.getAttachmentPaths(),userDoc.getAttachmentContent())), Store.YES, Index.ANALYZED));
		
		//不需要进行分词搜索
		doc.add(new Field(SearchConstant.NAME_BUSINESSSTRING, null2Blank(userDoc.getBusinessString()), Store.YES, Index.NOT_ANALYZED));	
		doc.add(new Field(SearchConstant.NAME_CREATETIME, SearchConstant.DATA_FORMAT.format((userDoc.getCreateTime() == null? new Date() : userDoc.getCreateTime())), Store.YES, Index.NOT_ANALYZED));
		doc.add(new Field(SearchConstant.NAME_TYPECODE, null2Blank(userDoc.getTypeCode()), Store.YES, Index.NOT_ANALYZED));
		doc.add(new Field(SearchConstant.NAME_ISDELETE, null2Blank(userDoc.getIsDelete()), Store.YES, Index.NOT_ANALYZED));
		
		return doc;
	}
	
	private String null2Blank(String src) {
		return src == null? "" : src;
	}
	
	private String getAttachmentContent(String[] attachmentPaths, String attachmentContent) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(attachmentContent);
		if (attachmentPaths != null) {
			for (int i = 0 ; i < attachmentPaths.length; i ++) {
				buffer.append(getText(attachmentPaths[i]));
			}
		}
		return buffer.toString();
	}
	
	private String getText(String path) {
		String value = (String) cache.get(path);
		if (value != null) return value;
		try {
			value = fileHandler.getText(new File(path));
			cache.put(path, value);
		} catch (FileHandlerException e) {
			System.err.println("文件解析错误");
			e.printStackTrace();
		}
		return value;
	}
	
	
}
